﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchoolLibrary.Entity.Person;

namespace SchoolLibrary.DataBase.DAO
{
    public class ClassLeaderDAO : AbstractDAO<ClassLeader>
    {
        private static ClassLeaderDAO instance;

        private ClassLeaderDAO() { }

        public static ClassLeaderDAO getInstance()
        {
            if (instance == null)
            {
                instance = new ClassLeaderDAO();
            }
            return instance;
        }

        public override ClassLeader findById(long id)
        {
            Query classLeaderQuery = new Query(ClassLeader.FIND_BY_ID);
            classLeaderQuery.setParameter("id", id);
            return getDAOHelper().getSingleResult(classLeaderQuery);
        }

        public override List<ClassLeader> findAll()
        {
            Query classLeaderQuery = new Query(ClassLeader.FIND_ALL);
            return getDAOHelper().getResultList(classLeaderQuery);
        }

        public override void save(ClassLeader classLeader)
        {
            if (classLeader.Id != UNDIFINED_ID)
            {
                save(classLeader, Person.UPDATE);
            }
            else
            {
                save(classLeader, Person.SAVE);
            }
        }

        private void save(Person person, string queryString)
        {
            Query query = new Query(queryString);
            query.setParameter("id", person.Id);
            query.setStringParameter("tableName", ClassLeader.TABLE_NAME);
            query.setParameter("name", person.Name);
            query.setParameter("middleName", person.MiddleName);
            query.setParameter("surname", person.Surname);
            getDAOHelper().executeUpdate(query);
        }
    }
}
